package com.sl.hx.shop.ui.mine.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.bean.HeadBean;
import com.sl.hx.shop.bean.ImageUrlBean;
import com.sl.hx.shop.bean.NickName;
import com.sl.hx.shop.bean.eventSetPhone;
import com.sl.hx.shop.ui.mine.contacts.SettingContacts;
import com.sl.hx.shop.ui.mine.presnter.SettingPresenter;
import com.sl.hx.shop.utils.NumberUtils;
import com.sl.hx.shop.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class SetPhoneActivity extends BaseActivity<SettingContacts.view, SettingPresenter> implements SettingContacts.view {


    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.save)
    TextView save;
    @BindView(R.id.yzm)
    TextView yzm;

    private String mphone;
    private String mYzm;
    private String backphone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setphone;
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter(mContext);
    }

    @Override
    protected void initView() {
        initEdit();

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_back, R.id.yzm, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            //获取验证码
            case R.id.yzm:
                mphone = editPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mphone)) {
                    ToastUtil.showShort(mContext, "手机号不能为空，请重新输入");
                } else if (!NumberUtils.isPhoneNum(mphone)) {
                    ToastUtil.showShort(mContext, "请输入正确的手机号");
                } else {
                    //获取验证码
                    mPresenter.getCode(yzm, mphone);
                }
                break;
            case R.id.save:
                mphone = editPhone.getText().toString().trim();
                mYzm = editCode.getText().toString().trim();
                if (TextUtils.isEmpty(mphone)) {
                    ToastUtil.showShort(mContext, "手机号不能为空，请重新输入");
                } else if (!NumberUtils.isPhoneNum(mphone)) {
                    ToastUtil.showShort(mContext, "请输入正确的手机号");
                } else if (TextUtils.isEmpty(mYzm)) {
                    ToastUtil.showShort(mContext, "验证码不能为空，请重新输入");
                } else {
                    mPresenter.setPhone(mphone, mYzm);
                }
                break;
        }
    }

    @Override
    public void showtoast(String message) {
        ToastUtil.showShort(mContext, message + "");
    }

    @Override
    public void setphoneSuccess(String mphone) {
        ToastUtil.showShort(mContext, "修改成功");
        eventSetPhone phone = new eventSetPhone(mphone);
        EventBus.getDefault().post(phone);
    }

    private void initEdit() {
        editCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (i2>0){
                    save.setClickable(true);
                    save.setBackgroundResource(R.drawable.setphone_redbtn);
                }else {
                    save.setClickable(true);
                    save.setBackgroundResource(R.drawable.setphone_whitebtn);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void changeHeadImgSuccess(ImageUrlBean imageUrlBean) {

    }

    @Override
    public void ShowUserInfo(HeadBean headBean) {

    }


}
